package com.ding.service;

import com.alibaba.fastjson.JSON;
import com.ding.api.Di_todoFeign;
import com.ding.dto.Di_messageDto;
import com.ding.dto.Di_todoDto;
import com.plf.common.domain.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author xiongzhiwen
 * @version 1.0
 * @date 2024/5/16 16:01
 */

@Slf4j
@Service
public class Di_todoRemoteService {


    @Value("${spring.profiles.active}")
    private String env;

    @Value("${sfding.open}")
    private boolean open;

    @Resource
    private Di_todoFeign di_todoFeign;

    private final String PRODENV = "sfprod";

    public void afterValidateSendToDo(Di_todoDto tododto) {
        // 判断当前的启动环境是否是prod
        if (StringUtils.isNotEmpty(env) && PRODENV.equalsIgnoreCase(env) && open) {
            ResponseDTO res = di_todoFeign.sendToDo(tododto);
            if(0 != res.getCode()) {
                log.error("赣政通待办任务发送失败 -> {}", JSON.toJSONString(tododto));
                throw new RuntimeException("赣政通待办任务发送失败");
            }
        } else {
            log.warn("当前启动环境为{}，不能发送赣政通待办", env);
        }
    }

    public void afterValidateSendMessage(Di_messageDto message) {
        // 判断当前的启动环境是否是prod
        if (StringUtils.isNotEmpty(env) && PRODENV.equalsIgnoreCase(env)  && open) {
            ResponseDTO res = di_todoFeign.sendMessage(message);
            if (0 != res.getCode()) {
                log.error("赣政通消息通知发送失败 -> {}", JSON.toJSONString(message));
                throw new RuntimeException("赣政通消息通知发送失败");
            }
        } else {
            log.warn("当前启动环境为{}，不能发送赣政通消息通知", env);
        }
    }


    public void afterValidateFinishTodo(String id) {
        // 判断当前的启动环境是否是prod
        if (StringUtils.isNotEmpty(env) && PRODENV.equalsIgnoreCase(env)  && open) {
            ResponseDTO res = di_todoFeign.finishTodo(id);
            if (0 != res.getCode()) {
                log.error("赣政通待办任务完成失败 -> {}", id);
                throw new RuntimeException("赣政通待办任务完成失败");
            }
        } else {
            log.warn("当前启动环境为{}，不能完成赣政通待办任务", env);
        }
    }


}
